
# -*- coding: utf-8 -*-
import sys
sys.path.append('../..')
import execjs
from itertools import product
from common.helper import Helper
import requests
from urllib.parse import urlencode
from db.mysql import MysqlClient
from common.config import *



class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "e享人生百万医疗plus保险"
    # 生成产品数据

    def genProductInfo(self):
        plan = {
            "year": [str(i) for i in range(0, 61)],
            "socialSec": ["N", "Y"],
            "payType": ["年交"],
            "expire": ["1年"],
            "generalMedical": ["3000000"],
            "seriousMedical": ["3000000"],
        }
        for v in product(plan["year"], plan["socialSec"], plan["payType"], plan["expire"], plan["generalMedical"], plan["seriousMedical"]):
            data = Helper.formatProductInfo(plan, v)
            self.mysql.cursor.execute(
                domain[self.name]["insert_sql"].format(**data))
            self.mysql._conn.commit()
            print(domain[self.name]["insert_sql"].format(**data))
    # 计算保费

    def calcPremium(self, dt=None, mysql=None):
        js = '''let payFormulaList = [
        {
            "payFormulaKey": "Y_0_1",
            "payFormulaValue": "736.00"
        },
        {
            "payFormulaKey": "N_0_2",
            "payFormulaValue": "1516.00"
        },
        {
            "payFormulaKey": "Y_1_1",
            "payFormulaValue": "736.00"
        },
        {
            "payFormulaKey": "N_1_2",
            "payFormulaValue": "1516.00"
        },
        {
            "payFormulaKey": "Y_2_1",
            "payFormulaValue": "736.00"
        },
        {
            "payFormulaKey": "N_2_2",
            "payFormulaValue": "1516.00"
        },
        {
            "payFormulaKey": "Y_3_1",
            "payFormulaValue": "736.00"
        },
        {
            "payFormulaKey": "N_3_2",
            "payFormulaValue": "1516.00"
        },
        {
            "payFormulaKey": "Y_4_1",
            "payFormulaValue": "736.00"
        },
        {
            "payFormulaKey": "N_4_2",
            "payFormulaValue": "1516.00"
        },
        {
            "payFormulaKey": "Y_5_1",
            "payFormulaValue": "736.00"
        },
        {
            "payFormulaKey": "N_5_2",
            "payFormulaValue": "1516.00"
        },
        {
            "payFormulaKey": "Y_6_1",
            "payFormulaValue": "236.00"
        },
        {
            "payFormulaKey": "N_6_2",
            "payFormulaValue": "458.00"
        },
        {
            "payFormulaKey": "Y_7_1",
            "payFormulaValue": "236.00"
        },
        {
            "payFormulaKey": "N_7_2",
            "payFormulaValue": "458.00"
        },
        {
            "payFormulaKey": "Y_8_1",
            "payFormulaValue": "236.00"
        },
        {
            "payFormulaKey": "N_8_2",
            "payFormulaValue": "458.00"
        },
        {
            "payFormulaKey": "Y_9_1",
            "payFormulaValue": "236.00"
        },
        {
            "payFormulaKey": "N_9_2",
            "payFormulaValue": "458.00"
        },
        {
            "payFormulaKey": "Y_10_1",
            "payFormulaValue": "236.00"
        },
        {
            "payFormulaKey": "N_10_2",
            "payFormulaValue": "458.00"
        },
        {
            "payFormulaKey": "Y_11_1",
            "payFormulaValue": "199.00"
        },
        {
            "payFormulaKey": "N_11_2",
            "payFormulaValue": "384.00"
        },
        {
            "payFormulaKey": "Y_12_1",
            "payFormulaValue": "199.00"
        },
        {
            "payFormulaKey": "N_12_2",
            "payFormulaValue": "384.00"
        },
        {
            "payFormulaKey": "Y_13_1",
            "payFormulaValue": "199.00"
        },
        {
            "payFormulaKey": "N_13_2",
            "payFormulaValue": "384.00"
        },
        {
            "payFormulaKey": "Y_14_1",
            "payFormulaValue": "199.00"
        },
        {
            "payFormulaKey": "N_14_2",
            "payFormulaValue": "384.00"
        },
        {
            "payFormulaKey": "Y_15_1",
            "payFormulaValue": "199.00"
        },
        {
            "payFormulaKey": "N_15_2",
            "payFormulaValue": "384.00"
        },
        {
            "payFormulaKey": "Y_16_1",
            "payFormulaValue": "194.00"
        },
        {
            "payFormulaKey": "N_16_2",
            "payFormulaValue": "371.00"
        },
        {
            "payFormulaKey": "Y_17_1",
            "payFormulaValue": "194.00"
        },
        {
            "payFormulaKey": "N_17_2",
            "payFormulaValue": "371.00"
        },
        {
            "payFormulaKey": "Y_18_1",
            "payFormulaValue": "194.00"
        },
        {
            "payFormulaKey": "N_18_2",
            "payFormulaValue": "371.00"
        },
        {
            "payFormulaKey": "Y_19_1",
            "payFormulaValue": "194.00"
        },
        {
            "payFormulaKey": "N_19_2",
            "payFormulaValue": "371.00"
        },
        {
            "payFormulaKey": "Y_20_1",
            "payFormulaValue": "194.00"
        },
        {
            "payFormulaKey": "N_20_2",
            "payFormulaValue": "371.00"
        },
        {
            "payFormulaKey": "Y_21_1",
            "payFormulaValue": "233.00"
        },
        {
            "payFormulaKey": "N_21_2",
            "payFormulaValue": "457.00"
        },
        {
            "payFormulaKey": "Y_22_1",
            "payFormulaValue": "233.00"
        },
        {
            "payFormulaKey": "N_22_2",
            "payFormulaValue": "457.00"
        },
        {
            "payFormulaKey": "Y_23_1",
            "payFormulaValue": "233.00"
        },
        {
            "payFormulaKey": "N_23_2",
            "payFormulaValue": "457.00"
        },
        {
            "payFormulaKey": "Y_24_1",
            "payFormulaValue": "233.00"
        },
        {
            "payFormulaKey": "N_24_2",
            "payFormulaValue": "457.00"
        },
        {
            "payFormulaKey": "Y_25_1",
            "payFormulaValue": "233.00"
        },
        {
            "payFormulaKey": "N_25_2",
            "payFormulaValue": "457.00"
        },
        {
            "payFormulaKey": "Y_26_1",
            "payFormulaValue": "332.00"
        },
        {
            "payFormulaKey": "N_26_2",
            "payFormulaValue": "673.00"
        },
        {
            "payFormulaKey": "Y_27_1",
            "payFormulaValue": "332.00"
        },
        {
            "payFormulaKey": "N_27_2",
            "payFormulaValue": "673.00"
        },
        {
            "payFormulaKey": "Y_28_1",
            "payFormulaValue": "332.00"
        },
        {
            "payFormulaKey": "N_28_2",
            "payFormulaValue": "673.00"
        },
        {
            "payFormulaKey": "Y_29_1",
            "payFormulaValue": "332.00"
        },
        {
            "payFormulaKey": "N_29_2",
            "payFormulaValue": "673.00"
        },
        {
            "payFormulaKey": "Y_30_1",
            "payFormulaValue": "332.00"
        },
        {
            "payFormulaKey": "N_30_2",
            "payFormulaValue": "673.00"
        },
        {
            "payFormulaKey": "Y_31_1",
            "payFormulaValue": "442.00"
        },
        {
            "payFormulaKey": "N_31_2",
            "payFormulaValue": "951.00"
        },
        {
            "payFormulaKey": "Y_32_1",
            "payFormulaValue": "442.00"
        },
        {
            "payFormulaKey": "N_32_2",
            "payFormulaValue": "951.00"
        },
        {
            "payFormulaKey": "Y_33_1",
            "payFormulaValue": "442.00"
        },
        {
            "payFormulaKey": "N_33_2",
            "payFormulaValue": "951.00"
        },
        {
            "payFormulaKey": "Y_34_1",
            "payFormulaValue": "442.00"
        },
        {
            "payFormulaKey": "N_34_2",
            "payFormulaValue": "951.00"
        },
        {
            "payFormulaKey": "Y_35_1",
            "payFormulaValue": "442.00"
        },
        {
            "payFormulaKey": "N_35_2",
            "payFormulaValue": "951.00"
        },
        {
            "payFormulaKey": "Y_36_1",
            "payFormulaValue": "604.00"
        },
        {
            "payFormulaKey": "N_36_2",
            "payFormulaValue": "1429.00"
        },
        {
            "payFormulaKey": "Y_37_1",
            "payFormulaValue": "604.00"
        },
        {
            "payFormulaKey": "N_37_2",
            "payFormulaValue": "1429.00"
        },
        {
            "payFormulaKey": "Y_38_1",
            "payFormulaValue": "604.00"
        },
        {
            "payFormulaKey": "N_38_2",
            "payFormulaValue": "1429.00"
        },
        {
            "payFormulaKey": "Y_39_1",
            "payFormulaValue": "604.00"
        },
        {
            "payFormulaKey": "N_39_2",
            "payFormulaValue": "1429.00"
        },
        {
            "payFormulaKey": "Y_40_1",
            "payFormulaValue": "604.00"
        },
        {
            "payFormulaKey": "N_40_2",
            "payFormulaValue": "1429.00"
        },
        {
            "payFormulaKey": "Y_41_1",
            "payFormulaValue": "809.00"
        },
        {
            "payFormulaKey": "N_41_2",
            "payFormulaValue": "2082.00"
        },
        {
            "payFormulaKey": "Y_42_1",
            "payFormulaValue": "809.00"
        },
        {
            "payFormulaKey": "N_42_2",
            "payFormulaValue": "2082.00"
        },
        {
            "payFormulaKey": "Y_43_1",
            "payFormulaValue": "809.00"
        },
        {
            "payFormulaKey": "N_43_2",
            "payFormulaValue": "2082.00"
        },
        {
            "payFormulaKey": "Y_44_1",
            "payFormulaValue": "809.00"
        },
        {
            "payFormulaKey": "N_44_2",
            "payFormulaValue": "2082.00"
        },
        {
            "payFormulaKey": "Y_45_1",
            "payFormulaValue": "809.00"
        },
        {
            "payFormulaKey": "N_45_2",
            "payFormulaValue": "2082.00"
        },
        {
            "payFormulaKey": "Y_46_1",
            "payFormulaValue": "1040.00"
        },
        {
            "payFormulaKey": "N_46_2",
            "payFormulaValue": "2887.00"
        },
        {
            "payFormulaKey": "Y_47_1",
            "payFormulaValue": "1040.00"
        },
        {
            "payFormulaKey": "N_47_2",
            "payFormulaValue": "2887.00"
        },
        {
            "payFormulaKey": "Y_48_1",
            "payFormulaValue": "1040.00"
        },
        {
            "payFormulaKey": "N_48_2",
            "payFormulaValue": "2887.00"
        },
        {
            "payFormulaKey": "Y_49_1",
            "payFormulaValue": "1040.00"
        },
        {
            "payFormulaKey": "N_49_2",
            "payFormulaValue": "2887.00"
        },
        {
            "payFormulaKey": "Y_50_1",
            "payFormulaValue": "1040.00"
        },
        {
            "payFormulaKey": "N_50_2",
            "payFormulaValue": "2887.00"
        },
        {
            "payFormulaKey": "Y_51_1",
            "payFormulaValue": "1340.00"
        },
        {
            "payFormulaKey": "N_51_2",
            "payFormulaValue": "3785.00"
        },
        {
            "payFormulaKey": "Y_52_1",
            "payFormulaValue": "1340.00"
        },
        {
            "payFormulaKey": "N_52_2",
            "payFormulaValue": "3785.00"
        },
        {
            "payFormulaKey": "Y_53_1",
            "payFormulaValue": "1340.00"
        },
        {
            "payFormulaKey": "N_53_2",
            "payFormulaValue": "3785.00"
        },
        {
            "payFormulaKey": "Y_54_1",
            "payFormulaValue": "1340.00"
        },
        {
            "payFormulaKey": "N_54_2",
            "payFormulaValue": "3785.00"
        },
        {
            "payFormulaKey": "Y_55_1",
            "payFormulaValue": "1340.00"
        },
        {
            "payFormulaKey": "N_55_2",
            "payFormulaValue": "3785.00"
        },
        {
            "payFormulaKey": "Y_56_1",
            "payFormulaValue": "1747.00"
        },
        {
            "payFormulaKey": "N_56_2",
            "payFormulaValue": "4873.00"
        },
        {
            "payFormulaKey": "Y_57_1",
            "payFormulaValue": "1747.00"
        },
        {
            "payFormulaKey": "N_57_2",
            "payFormulaValue": "4873.00"
        },
        {
            "payFormulaKey": "Y_58_1",
            "payFormulaValue": "1747.00"
        },
        {
            "payFormulaKey": "N_58_2",
            "payFormulaValue": "4873.00"
        },
        {
            "payFormulaKey": "Y_59_1",
            "payFormulaValue": "1747.00"
        },
        {
            "payFormulaKey": "N_59_2",
            "payFormulaValue": "4873.00"
        },
        {
            "payFormulaKey": "Y_60_1",
            "payFormulaValue": "1747.00"
        },
        {
            "payFormulaKey": "N_60_2",
            "payFormulaValue": "4873.00"
        }
        ]
        function calc(custom,rationNo){
            for(var i = 0; i < payFormulaList.length; i++){
                var payKey =payFormulaList[i].payFormulaKey;
                var payValue =payFormulaList[i].payFormulaValue;
                var payKeyOne = payFormulaList[i].payFormulaKey.split("_")[0];
                var payKeyTwo = payFormulaList[i].payFormulaKey.split("_")[1];
                var payKeyThr = payFormulaList[i].payFormulaKey.split("_")[2];
                if(payKeyOne == 'T'){
                    if(payKeyTwo == custom){
                        if(payKeyThr == rationNo){
                            $("#premium").val(payValue);
                            $("#money").html(payValue);
                            $("#money_premium").html(payValue);
                        }
                    }
                }else if(payKeyOne == 'Y'){
                    if(payKeyTwo == custom){
                        if(payKeyThr == rationNo){
                            return payValue;
                        }
                    }
                }else if(payKeyOne == 'N'){
                    if(payKeyTwo == custom){
                        if(payKeyThr == rationNo){
                            return payValue;
                        }
                    }
                }else if(payKeyOne == 'F'){
                    if(payKeyTwo == custom){
                        if(payKeyThr == rationNo){
                            $("#premium").val(payValue);
                            $("#money").html(payValue);
                            $("#money_premium").html(payValue);
                        }
                    }
                }
            }
        }'''
        cxt = execjs.compile(js)
        age = int(dt["year"])
        social = "1" if dt["socialSec"] == "Y" else "2"
        price = cxt.call("calc", age, social)
        data = {
            "name": self.name,
            "id": dt["id"],
            "price": price
        }
        print( data )
        mysql.updatePrice( data )